import Column from "./Column";

let ranTime=1000,timer=0;
let zhuji;
let isGameover=true;
let columnArr=[];
    export default class zhu extends Laya.Script {

    constructor() { 
        super(); 
        /** @prop {name:zhu, tips:"布尔类型示例", type:prefab, default:null}*/
        let zhu=null;
        // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
    }
    onAwake(){
        
        zhuji=this.owner.getChildByName("zhuji");
        Laya.stage.on("Gameover",this,function(){isGameover=true})
        Laya.stage.on("Again",this,function(){
            isGameover=false
            columnArr.forEach(element => {
                element.removeSelf();
            });
            columnArr=[];
        })
        Laya.stage.on("Start",this,function(){
            isGameover=false;
        })
    }
    onUpdate(){
        if(isGameover){
            timer=0;
            return
        }
        timer+=Laya.timer.delta;
        if(timer>=ranTime){
            timer=0;
            ranTime=this.getRandom(1000,1500);
            this.spawn();
        }
    }
    spawn(){
        var bottom=Laya.Pool.getItemByCreateFun("Column",this.creatFun,this);
        zhuji.addChild(bottom);
        bottom.rotation=0;
        var bottomY=this.getRandom(300,660);
        bottom.pos(1920,bottomY);
        bottom.getComponent(Column).canAddScore=true;
        columnArr.push(bottom);

        var cha=this.getRandom(245,348);
        var topY=bottomY-cha;

        var top=Laya.Pool.getItemByCreateFun("Column",this.creatFun,this);
        zhuji.addChild(top);
        zhuji.addChild(top);
        top.rotation=180;
        top.pos(2176,topY);
        top.getComponent(Column).canAddScore=false;
        columnArr.push(top);
    }
    creatFun(){
        var temp=this.zhu.create();
        return temp;
    }
    getRandom(min,max){
        var ranValue=0;
        if(max>min){
            ranValue=Math.random()*(max-min);
            ranValue+=min;
        }else{
            ranValue=Math.random()*(max-min);
            ranValue+=max;
        }
        return ranValue;
    }

}